package com.final_project.zombiemenace.model;

import android.content.Context;
import android.content.Intent;
import android.graphics.Matrix;
import android.os.Vibrator;

import com.final_project.zombiemenace.SaveHighScore;
import com.final_project.zombiemenace.highscore.HighScoreHandler;
import com.final_project.zombiemenace.screens.GameScreenZombie;
import com.final_project.zombiemenace.settings.Settings;
import com.kilobolt.framework.Game;

public class Player {
	private static final int VIBRATION_DURATION = 400;
	private static final int MAX_SCREEN_HEIGHT = 480;
	private static final int MAX_SCREEN_WIDTH = 800;
	private static final int FIX_BULLET_Y_COORD = 35;
	private static final int FIX_BULLET_X_COORD = 5;
	private static final float FIX_PLAYER_CIRCLE_RADIUS = 1.5f;
	private static final int PLAYER_SPEED_FIXER = 7;
	private int health;
	private int points;
	private boolean isAlive;
	private float centerX;
	private float centerY;
	private float speedX = 0;
	private float speedY = 0;
	public Circle playerCircle = new Circle();

	public Player(int health, float centerX, float centerY) {
		setHealth(health);
		setCenterX(centerX);
		setCenterY(centerY);
		setAlive(true);
		setPoints(0);
	}

	public void update() {
		move();
		playerCircle.setCenterX(centerX);
		playerCircle.setCenterY(centerY);
		playerCircle.setRadius(Assets.character.getWidth()
				/ FIX_PLAYER_CIRCLE_RADIUS);
		if (isAlive && this.health <= 0) {
			setAlive(false);
		}
	}

	public boolean isPlayerDead(Game game) {
		if (!this.isAlive()) {
			Assets.characterDies.play(80.0f);
			Settings settings = new Settings(game.getContext());
			if(settings.getIsGoingToVibrateFromSharedPrefs()){
				Vibrator vibrator = (Vibrator) game.getContext().getSystemService(
						Context.VIBRATOR_SERVICE);
				vibrator.vibrate(VIBRATION_DURATION);
			}
			openSaveHighScoreScreen(game);
			return true;
		}
		return false;
	}

	private void openSaveHighScoreScreen(Game game) {
		HighScoreHandler hsh = new HighScoreHandler(game.getContext());
		if (hsh.shouldBeInHighScore(this.getPoints())) {
			Intent saveHighScore = new Intent(game.getContext(),
					SaveHighScore.class);
			saveHighScore.putExtra("score", this.getPoints());
			game.getContext().startActivity(saveHighScore);
		}
	}
	public Matrix updateRotation() {
		float rotationDegrees = (float) (GameScreenZombie.angleRotation * (180 / Math.PI));
		Matrix matrix = new Matrix();
		matrix.reset();
		matrix.postRotate(rotationDegrees);
		return matrix;
	}

	public void updateSpeed(int centerPointXLeftJoystick,
			int initXLeftJoystick, int centerPointYLeftJoystick,
			int initYLeftJoystick) {
		if (centerPointXLeftJoystick < initXLeftJoystick) {
			setSpeedX((centerPointXLeftJoystick - initXLeftJoystick)
					/ PLAYER_SPEED_FIXER);
		} else if (centerPointXLeftJoystick > initXLeftJoystick) {
			setSpeedX((centerPointXLeftJoystick - initXLeftJoystick)
					/ PLAYER_SPEED_FIXER);
		} else if (centerPointXLeftJoystick == initXLeftJoystick) {
			setSpeedX(0);
		}

		if (centerPointYLeftJoystick < initYLeftJoystick) {
			setSpeedY((centerPointYLeftJoystick - initYLeftJoystick)
					/ PLAYER_SPEED_FIXER);
		} else if (centerPointYLeftJoystick > initYLeftJoystick) {
			setSpeedY((centerPointYLeftJoystick - initYLeftJoystick)
					/ PLAYER_SPEED_FIXER);
		} else if (centerPointYLeftJoystick == initYLeftJoystick) {
			setSpeedY(0);
		}
	}

	public Bullet attack(float angleOfRotation) {
		float playerScaledX = getCenterX() - (Assets.character.getWidth() / 2);
		float playerScaledY = getCenterY() - (Assets.character.getHeight() / 2);
		float bulletX = playerScaledX + (Assets.character.getWidth() / 2);
		float bulletY = playerScaledY;
		double distance = Math.sqrt(Math.pow(bulletX - playerScaledX, 2)
				+ Math.pow(bulletY - playerScaledY, 2));
		float angledX = (float) (bulletX - FIX_BULLET_X_COORD + (Math
				.cos(angleOfRotation) * distance));
		float angledY = (float) (bulletY + FIX_BULLET_Y_COORD + (Math
				.sin(angleOfRotation) * distance));
		return new Bullet(angledX, angledY, angleOfRotation);
	}

	/**
	 * Method that gets coordinates from left joystick and changes coordX and
	 * coordY with speedX and speedY
	 */
	public void move() {
		if (this.centerX + this.speedX + Assets.character.getWidth() / 2 < MAX_SCREEN_WIDTH
				&& this.centerX + this.speedX - Assets.character.getWidth() / 2 > 0) {
			this.centerX += this.speedX;
		}
		if (this.centerY + this.speedY + Assets.character.getHeight() / 2 < MAX_SCREEN_HEIGHT
				&& this.centerY + this.speedY - Assets.character.getHeight()
						/ 2 > 0) {
			this.centerY += this.speedY;
		}

	}

	public int getHealth() {
		return health;
	}

	public void setHealth(int health) {
		this.health = health;
	}

	public int getPoints() {
		return points;
	}

	public void setPoints(int points) {
		this.points = points;
	}

	public boolean isAlive() {
		return isAlive;
	}

	public void setAlive(boolean isAlive) {
		this.isAlive = isAlive;
	}

	public float getCenterX() {
		return centerX;
	}

	public void setCenterX(float centerX) {
		this.centerX = centerX;
	}

	public float getCenterY() {
		return centerY;
	}

	public void setCenterY(float centerY) {
		this.centerY = centerY;
	}

	public float getSpeedX() {
		return speedX;
	}

	public void setSpeedX(float speedX) {
		this.speedX = speedX;
	}

	public float getSpeedY() {
		return speedY;
	}

	public void setSpeedY(float speedY) {
		this.speedY = speedY;
	}

	public void resetPlayerSpeed() {
		setSpeedX(0);
		setSpeedY(0);
	}
}
